﻿@page "/wallpaper"

<div class="wallpaper-container">
    @if (wallpapers == null)
    {
        <Loading />
    }
    else
    {
        <div class="plus-container">
            <p class="wallpaper-nav">
                @if (types != null)
                {
                    <NavLink class="@(type == 0 ? "wallpaper-active" : null)" @onclick="(async () => await ChangeType(0))">全部</NavLink>
                    @foreach (var item in types.Result)
                    {
                        <NavLink class="@(item.Value == type ? "wallpaper-active" : null)" @onclick="@(async () => await ChangeType(item.Value))">@item.Description</NavLink>
                    }
                }
            </p>
            <div class="wallpapers plus-row plus-col-space20">
                @foreach (var item in wallpapers.Result.Item)
                {
                    <div class="plus-col-xs6 plus-col-md2">
                        <div class="wallpaper">
                            <img src="@(item.Url.Replace("max", "middle"))" title="@item.Title" alt="@item.Title" @onclick="@(async () => await openWallpaper(item.Url))" />
                        </div>
                    </div>
                }
            </div>
        </div>
        <nav class="pagination">
            @if (page == 1)
            {
                <span class="page-number">Previous</span>
                <NavLink class="page-number" @onclick="@(async () => await ChangePage(page + 1))">Next</NavLink>
            }
            else if (page >= TotalPage)
            {
                <NavLink class="page-number" @onclick="@(async () => await ChangePage(page - 1))">Previous</NavLink>
                <span class="page-number">Next</span>
            }
            else
            {
                <NavLink class="page-number" @onclick="@(async () => await ChangePage(page - 1))">Previous</NavLink>
                <NavLink class="page-number" @onclick="@(async () => await ChangePage(page + 1))">Next</NavLink>
            }
        </nav>
    }
</div>

@code {
    /// <summary>
    /// 类型
    /// </summary>
    private int type { get; set; }

    /// <summary>
    /// 关键字
    /// </summary>
    private string keyword { get; set; }

    /// <summary>
    /// 当前页
    /// </summary>
    private int page { get; set; }

    /// <summary>
    /// 返回数量
    /// </summary>
    private int Limit = 30;

    /// <summary>
    /// 总页码
    /// </summary>
    private int TotalPage;

    /// <summary>
    /// 壁纸分类
    /// </summary>
    private ServiceResult<IEnumerable<EnumResponse>> types;

    /// <summary>
    /// 壁纸列表
    /// </summary>
    private ServiceResult<PagedList<WallpaperDto>> wallpapers;

    /// <summary>
    /// 初始化
    /// </summary>
    /// <returns></returns>
    protected override async Task OnInitializedAsync()
    {
        await Common.SetTitleAsync("手机壁纸");

        var uri = await Common.CurrentUri();
        var query = QueryHelpers.ParseQuery(uri.Query);

        var hasType = query.TryGetValue("t", out Microsoft.Extensions.Primitives.StringValues t);
        var hasKeyword = query.TryGetValue("k", out Microsoft.Extensions.Primitives.StringValues k);
        var hasPage = query.TryGetValue("p", out Microsoft.Extensions.Primitives.StringValues p);

        if (!int.TryParse(t, out int _type) || !hasType)
        {
            _type = 0;
        }
        if (!int.TryParse(p, out int _page) || !hasPage)
        {
            _page = 1;
        }

        type = _type;
        page = _page;
        keyword = hasKeyword ? k.ToString() : "";

        await SettingRouteAsync();
        await GetWallpaperTypesAsync();
        await QueryWallpapersAsync();

        await Common.InvokeAsync("window.func.disableKey");
    }

    /// <summary>
    /// 获取壁纸分类数据
    /// </summary>
    /// <returns></returns>
    private async Task GetWallpaperTypesAsync()
    {
        types = await Http.GetFromJsonAsync<ServiceResult<IEnumerable<EnumResponse>>>("/wallpaper/types");
    }

    /// <summary>
    /// 获取壁纸列表
    /// </summary>
    /// <returns></returns>
    private async Task QueryWallpapersAsync()
    {
        wallpapers = await Http.GetFromJsonAsync<ServiceResult<PagedList<WallpaperDto>>>($"/wallpaper?type={type}&keywords={keyword}&page={page}&limit={Limit}");

        // 计算总页码
        TotalPage = (int)Math.Ceiling((wallpapers.Result.Total / (double)Limit));
    }

    /// <summary>
    /// 类型点击事件
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    private async Task ChangeType(int value)
    {
        if (value == type) return;

        wallpapers = null;
        type = value;
        page = 1;

        await SettingRouteAsync();
        await QueryWallpapersAsync();
    }

    /// <summary>
    /// 翻页点击事件
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    private async Task ChangePage(int value)
    {
        wallpapers = null;
        page = value;

        await SettingRouteAsync();
        await QueryWallpapersAsync();
    }

    /// <summary>
    /// 点击打开壁纸图片
    /// </summary>
    /// <param name="src"></param>
    /// <returns></returns>
    private async Task openWallpaper(string src)
    {
        await Common.InvokeAsync("window.func.openBlobWallpaper", src);
    }

    /// <summary>
    /// 设置路由
    /// </summary>
    /// <returns></returns>
    private async Task SettingRouteAsync()
    {
        await Common.NavigateTo($"/wallpaper?t={type}&k={keyword}&p={page}");
    }
}